/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.bodylimit.dao;

import br.com.bodylimit.model.Exercicio;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jõao Paulo
 */
public class ExercicioDAOPostgres implements ExercicioDAO{

    Connection con;
    public ExercicioDAOPostgres(Connection con) {
        this.con = con;
    }

    private List<Exercicio> popular(ResultSet rs) throws SQLException {
        List<Exercicio> exercicios = new ArrayList<Exercicio>(0);
        PreparedStatement stmt;
        ResultSet rsTel;
        while (rs.next()) {
            Exercicio exercicio = new Exercicio();

            //Atribuindo valoles
            exercicio.setId(rs.getInt("id"));
            exercicio.setNome(rs.getString("nome"));
            exercicio.setDescricao(rs.getString("descricao"));

            exercicios.add(exercicio);


        }
        return exercicios;
    }


    public Exercicio obter(int id) throws SQLException {
        String sql = "SELECT id, nome, descricao FROM exercicio WHERE id = ?";

        List<Exercicio> exercicios;
        ResultSet rs;
        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setInt(1, id);
        rs = stmt.executeQuery();

        exercicios = popular(rs);

        rs.close();
        stmt.close();

        return exercicios.isEmpty() ? null : exercicios.get(0);
    }

    public List<Exercicio> listarPorNome(String nome) throws SQLException {
        String sql = "SELECT id, nome, descricao FROM exercicio WHERE nome LIKE ?";

        List<Exercicio> exercicios;
        ResultSet rs;
        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setString(1, nome);
        rs = stmt.executeQuery();

        exercicios = popular(rs);

        rs.close();
        stmt.close();

        return exercicios;
    }

    public List<Exercicio> listar() throws SQLException {
                String sql = "SELECT id, nome, descricao FROM exercicio";

        List<Exercicio> exercicios;
        ResultSet rs;
        PreparedStatement stmt = con.prepareStatement(sql);

        rs = stmt.executeQuery();

        exercicios = popular(rs);

        rs.close();
        stmt.close();

        return exercicios;
    }

    public void remover(int id) throws SQLException {
        String sql = "DELETE FROM exercicio WHERE id = ?";

        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setInt(1, id);
        stmt.executeUpdate();

        stmt.close();
    }

    public boolean atualizar(Exercicio exercicio) throws SQLException {
        if(exercicio.getId() == 0) {
            return false;
        }

        String sql = "UPDATE exercicio SET nome = ?, descricao = ? WHERE id = ?";

        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setString(1, exercicio.getNome());
        stmt.setString(2, exercicio.getDescricao());
        stmt.setInt(3, exercicio.getId());
        stmt.executeUpdate();

        stmt.close();
        return true;
    }

    public void adcionar(Exercicio Exercicio) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
